package com.wdz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wdz.friend_chain.entity.FriendChain;
import com.wdz.friend_chain.service.IFriendChainService;
import com.wdz.http.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/friend_chain")
public class FriendChainController {
    @Autowired
    private IFriendChainService iFriendChainService;

    @PostMapping("/")
    public Response add(@RequestBody FriendChain friendChain){iFriendChainService.insert(friendChain); return Response.success((Object) friendChain.getId()); }

    @GetMapping("/{pageNo}/{pageSize}")
    public Response get(@PathVariable int pageNo,@PathVariable int pageSize){
        Map<String,Object> data = new HashMap<>();
        data.put("total",iFriendChainService.selectCount(null));
        data.put("data",iFriendChainService.selectPage(new Page<>(pageNo,pageSize),new EntityWrapper<FriendChain>().orderBy("create_date",false)).getRecords());
        return Response.success(data);
    }
    @GetMapping("/getAdopt/{pageNo}/{pageSize}")
    public Response getAdopt(@PathVariable int pageNo,@PathVariable int pageSize){
        Map<String,Object> data = new HashMap<>();
        data.put("total",iFriendChainService.selectCount(new EntityWrapper<FriendChain>().where("status=1")));
        data.put("data",iFriendChainService.selectPage(new Page<>(pageNo,pageSize),new EntityWrapper<FriendChain>().where("status=1").orderBy("create_date",false)).getRecords());
        return Response.success(data);
    }

    @PutMapping("/")
    public Response update(@RequestBody FriendChain friendChain){
        iFriendChainService.updateById(friendChain);
        return Response.success();
    }

    @DeleteMapping("/{id}")
    public Response delete(@PathVariable String id){iFriendChainService.deleteById(id); return Response.success();}

}
